package com.ruoyi.dgdData.service.impl;

import java.text.DecimalFormat;
import java.util.List;
import java.util.concurrent.Future;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.dgdData.domain.pad.DataDgdModifyQyl;
import com.ruoyi.dgdData.domain.plat.DataDgdQyl;
import com.ruoyi.dgdData.mapper.DataDgdQylMapper;
import com.ruoyi.dgdData.service.IDataDgdQylService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;


/**
 * 单轨吊牵引力测试数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-28
 */
@Service
public class DataDgdQylServiceImpl implements IDataDgdQylService
{
    @Autowired
    private DataDgdQylMapper dataDgdQylMapper;

    /**
     * 查询单轨吊牵引力测试数据
     * 
     * @param qylId 单轨吊牵引力测试数据主键
     * @return 单轨吊牵引力测试数据
     */
    @Override
    public DataDgdQyl selectDataDgdQylByQylId(Long qylId)
    {
        return dataDgdQylMapper.selectDataDgdQylByQylId(qylId);
    }

    /**
     * 查询单轨吊牵引力测试数据
     *
     * @param dataTaskId
     * @return 单轨吊牵引力测试数据
     */
    @Override
    public Future<List<DataDgdQyl>> selectDataDgdQylByDataTaskId(Long dataTaskId) {
        return new AsyncResult<>(dataDgdQylMapper.selectDataDgdQylByDataTaskId(dataTaskId));
    }

    /**
     * 查询单轨吊牵引力测试数据列表
     * 
     * @param dataDgdQyl 单轨吊牵引力测试数据
     * @return 单轨吊牵引力测试数据
     */
    @Override
    public List<DataDgdQyl> selectDataDgdQylList(DataDgdQyl dataDgdQyl)
    {
        return dataDgdQylMapper.selectDataDgdQylList(dataDgdQyl);
    }

    /**
     * 新增单轨吊牵引力测试数据
     * 
     * @param dataDgdQyl 单轨吊牵引力测试数据
     * @return 结果
     */
    @Override
    public int insertDataDgdQyl(DataDgdQyl dataDgdQyl)
    {
        dataDgdQyl.setCreateTime(DateUtils.getNowDate());
        return dataDgdQylMapper.insertDataDgdQyl(dataDgdQyl);
    }

    /**
     * 批量新增牵引力测试
     *
     * @param dataDgdQyls
     */
    @Override
    public int batchDataDgdQyl(List<DataDgdQyl> dataDgdQyls) {
        return dataDgdQylMapper.batchDataDgdQyl(dataDgdQyls);
    }

    /**
     * 修改单轨吊牵引力测试数据
     * 
     * @param dataDgdQyl 单轨吊牵引力测试数据
     * @return 结果
     */
    @Override
    public int updateDataDgdQyl(DataDgdQyl dataDgdQyl)
    {
        dataDgdQyl.setUpdateTime(DateUtils.getNowDate());
        return dataDgdQylMapper.updateDataDgdQyl(dataDgdQyl);
    }

    /**
     * 变更单轨吊牵引力数据状态为2
     *
     * @param qylId
     * @return 结果
     */
    @Override
    public int updateDataDgdQylByQylId(Long qylId) {
        return dataDgdQylMapper.updateDataDgdQylByQylId(qylId);
    }

    /**
     * 变更单轨吊牵引力数据状态为2
     *
     * @param dataTaskId
     * @return 结果
     */
    @Override
    public int updateDataDgdQylByDataTaskId(Long dataTaskId) {
        return dataDgdQylMapper.updateDataDgdQylByDataTaskId(dataTaskId);
    }

    /**
     * 批量删除单轨吊牵引力测试数据
     * 
     * @param qylIds 需要删除的单轨吊牵引力测试数据主键
     * @return 结果
     */
    @Override
    public int deleteDataDgdQylByQylIds(Long[] qylIds)
    {
        return dataDgdQylMapper.deleteDataDgdQylByQylIds(qylIds);
    }

    /**
     * 删除单轨吊牵引力测试数据信息
     * 
     * @param qylId 单轨吊牵引力测试数据主键
     * @return 结果
     */
    @Override
    public int deleteDataDgdQylByQylId(Long qylId)
    {
        return dataDgdQylMapper.deleteDataDgdQylByQylId(qylId);
    }

    /**
     * 计算平均牵引力和差值
     *
     * @param dataDgdModifyQyl
     */
    @Override
    public DataDgdModifyQyl modifyQyl(DataDgdModifyQyl dataDgdModifyQyl) {
        //计算平均牵引力
        if(dataDgdModifyQyl.getQylList().isEmpty() || null == dataDgdModifyQyl.getQylList()) {
            return null;
        }else {
            float sumQyl = 0f;
            for(int i = 0; i < dataDgdModifyQyl.getQylList().size(); i++) {
                sumQyl += Float.valueOf(dataDgdModifyQyl.getQylList().get(i));
            }
            dataDgdModifyQyl.setPjqyl(String.format("%.3f", sumQyl/dataDgdModifyQyl.getQylList().size()));
            if(null != dataDgdModifyQyl.getEdqyl() && StringUtils.isNotEmpty(dataDgdModifyQyl.getEdqyl())) {
                //计算差值
                float chazhiFloat = Math.abs(Float.valueOf(dataDgdModifyQyl.getPjqyl()) - Float.valueOf(dataDgdModifyQyl.getEdqyl())) / Float.valueOf(dataDgdModifyQyl.getEdqyl());
                String chazhiStr = new DecimalFormat("0.0").format(chazhiFloat * 100) + "%";
                dataDgdModifyQyl.setChzh(chazhiStr);
            }
        }
        return dataDgdModifyQyl;
    }
}
